﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class Inventory_Targe_Maintain : PageBase
{
    private decimal id = 0;
    private const string CHANNEL_ID = "id";
    private const string RETURN_PAGE = "Inventory_Target_List.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[CHANNEL_ID] != null)
        {
            decimal.TryParse(Request.QueryString[CHANNEL_ID], out id);
        }
        if (!IsPostBack)
        {
            this.FillForm();
        }
    }

    private void FillForm()
    {
        if (id > 0)
        {
            ICO_PROFILES ico = new IcoProfileManager().Load(id);
            txtName.Text = ico.CODE;
            //txtPTotal.Text = ico.p.ToString("##.###");
            //txtSupplyA.Text = ico.DAYSSUPPLYA.ToString("##.###");
            //txtSupplyB.Text = ico.DAYSSUPPLYB.ToString("##.###");
            //txtInterval.Text = ico.LASTCOVERINTERVAL.ToString("##.###");
            //txtCSLLEVELFACTOR.Text = ico.CSLLEVELFACTOR.ToString("##.###");
            //cbxIsValid.Checked = ico.IsValid;
            //txtW0.Text = ico..ToString();
            //txtW1.Text = ico.WEIGHT1.ToString();
            //txtW2.Text = ico.WEIGHT2.ToString();
            //txtW3.Text = ico.WEIGHT3.ToString();
            //txtW4.Text = ico.WEIGHT4.ToString();
            //txtW5.Text = ico.WEIGHT5.ToString();
            //txtW6.Text = ico.WEIGHT6.ToString();
            //txtW7.Text = ico.WEIGHT7.ToString();
            //txtW8.Text = ico.WEIGHT8.ToString();
        }
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ICO_PROFILES ico = null;
            IcoProfileManager icoManager = new IcoProfileManager();
            if (id > 0)
            {
                ico = icoManager.Load(id);
            }
            else
            {
                ico = new ICO_PROFILES();
            }
          
            //ico.PTOTAL = decimal.Parse(txtPTotal.Text);
            //ico.DAYSSUPPLYB = decimal.Parse(txtSupplyB.Text);
            //ico.DAYSSUPPLYA = decimal.Parse(txtSupplyA.Text);
            //ico.LASTCOVERINTERVAL = decimal.Parse(txtInterval.Text);
            ico.CODE = txtName.Text;
            //ico.VALID = cbxIsValid.Checked ? "1" : "0";

            decimal outvalue;
            if (!decimal.TryParse(txtW0.Text, out outvalue))
            {
                ShowMessage("W0文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW1.Text, out outvalue))
            {
                ShowMessage("W1文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW2.Text, out  outvalue))
            {
                ShowMessage("W2文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW3.Text, out  outvalue))
            {
                ShowMessage("W3文本框只能输入数字");
                return;
            }


            if (!decimal.TryParse(txtW4.Text, out  outvalue))
            {
                ShowMessage("W4文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW5.Text, out outvalue))
            {
                ShowMessage("W5文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW6.Text, out outvalue))
            {
                ShowMessage("W6文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW7.Text, out  outvalue))
            {
                ShowMessage("W7文本框只能输入数字");
                return;
            }

            if (!decimal.TryParse(txtW8.Text, out  outvalue))
            {
                ShowMessage("W8文本框只能输入数字");
                return;
            }

            if (decimal.Parse(txtW0.Text) +
                decimal.Parse(txtW1.Text) +
                decimal.Parse(txtW2.Text) +
                decimal.Parse(txtW3.Text) +
                decimal.Parse(txtW4.Text) +
                decimal.Parse(txtW5.Text) +
                decimal.Parse(txtW6.Text) +
                decimal.Parse(txtW7.Text) +
                decimal.Parse(txtW8.Text) != 1)
            {
                throw new Exception("W0至W8之和应为1");
            }

            //ico.WEIGHT0 = decimal.Parse(txtW0.Text);
            //ico.WEIGHT1 = decimal.Parse(txtW1.Text);
            //ico.WEIGHT2 = decimal.Parse(txtW2.Text);
            //ico.WEIGHT3 = decimal.Parse(txtW3.Text);
            //ico.WEIGHT4 = decimal.Parse(txtW4.Text);
            //ico.WEIGHT5 = decimal.Parse(txtW5.Text);
            //ico.WEIGHT6 = decimal.Parse(txtW6.Text);
            //ico.WEIGHT7 = decimal.Parse(txtW7.Text);
            //ico.WEIGHT8 = decimal.Parse(txtW8.Text);
            //ico.CSLLEVELFACTOR = decimal.Parse(txtCSLLEVELFACTOR.Text);

            //ico.DOMAINID = MyStateManager.DomainID;
            //ico.LAST_UPDATE_TIME = DateTime.Now;
            //ico.LAST_UPDATE = MyStateManager.Name;

            icoManager.Save(ico);

            Response.Redirect(RETURN_PAGE);
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(RETURN_PAGE);
    }

}
